# -*- coding:utf-8 -*-
import ctypes

from django.contrib.auth.models import User
from django.contrib.auth import REDIRECT_FIELD_NAME, authenticate
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth.views import login
from django.core.urlresolvers import reverse_lazy
from django.views.generic.edit import UpdateView, CreateView
from django.views.generic.detail import DetailView

from forms import UserProfileForm, UserCreateForm


# Create your views here.
def login_with_domain(request,
                      redirect_field_name=REDIRECT_FIELD_NAME,
                      authentication_form=AuthenticationForm,
                      current_app=None, extra_context=None):
    return login(request, redirect_field_name, authentication_form, current_app, extra_context)


# ToDO "需要重新登录？ "
class UserRegisterView(CreateView):
    form_class = UserCreateForm
    template_name = "registration/register.html"
    success_url = reverse_lazy("index")

    def form_valid(self, form):
        result = super(UserRegisterView, self).form_valid(form)
        username = form.cleaned_data['username']
        password = form.cleaned_data['password1']
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(self.request, user)
        #         # Redirect to a success page.
        #     else:
        #         # Return a ’disabled account’ error message
        # else:
        #     # Return an ’invalid login’ error message.
        return result


# @csrf_protect
# def register(request):
#     """register a new user"""
#     if request.method == 'POST':
#         form = UserCreateForm(request.POST)
#         if form.is_valid():
#             new_user = form.save()
#             # new_user.groups.add(Group.objects.get(name="Normal_User"))
#             # return HttpResponseRedirect(reverse_lazy('registration:register'))
#             return HttpResponseRedirect(reverse_lazy('index'))
#     else:
#         form = UserCreateForm()
#     return render_to_response("registration/register.html", {'form': form}, context_instance=RequestContext(request))


class ProfileDetailView(DetailView):
    model = User
    template_name = "registration/profile.html"
    context_object_name = 'user'

    def get_object(self):
        object = self.request.user
        return object


# "view profile of login user"
# @csrf_protect
# def profile(request):
#     user = request.user
#     return render_to_response("registration/profile.html", {'user': user,})


class ProfileUpdateView(UpdateView):
    form_class = UserProfileForm
    template_name = 'registration/profile_change.html'
    success_url = reverse_lazy("registration:profile")

    def get_object(self):
        object = self.request.user
        return object


# "edit login user profile"
# @csrf_protect
# def profile_change(request):
#     form = UserProfileForm(instance=request.user)
#
#     if request.method == 'POST':
#         form = UserProfileForm(request.POST,instance=request.user)
#         if form.is_valid():
#             new_user = form.save()
#             return HttpResponseRedirect(reverse_lazy('registration:profile'))
#     return render_to_response("registration/profile_change.html", {'form': form,},context_instance=RequestContext(request))